package ch.zbw.erp.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class LoginPass  extends JFrame implements ActionListener{
	
	private static final long serialVersionUID =1L;
	private static final int FRAMEHEIGHT = 500;
	private static final int FRAMEWIDTH = 600;
	
	private Kontroller control;
	
	private JPanel obenImage;
	private JPanel untenPass;
	private JPanel users;
	private JPanel pass;
	private JPanel jPlogin;
	private JLabel image;
	private JLabel falsch;
	
	private JPasswordField passwordField;
	
	private ButtonGroup buttonGroupUsers;
	private JButton login;
	private JRadioButton mitarbeiter;
	private JRadioButton abteilungsleiter;
	private JRadioButton firmenleiter;
	private ImageIcon erplogo;
	
	public LoginPass(Kontroller c){
		super("Login ERP");
		control=c;
		initialLoginPass();
		
	}
	
	public void initialLoginPass(){
		this.setBackground(Color.lightGray);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(FRAMEWIDTH, FRAMEHEIGHT);
		this.setResizable(false);
		
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		int screenWidtht = Toolkit.getDefaultToolkit().getScreenSize().width;
		this.setLocation(screenWidtht / 2 - FRAMEWIDTH / 2, screenHeight / 2 -FRAMEHEIGHT /2);
		
		//set Panels & Buttons
		obenImage = new JPanel();
		untenPass = new JPanel();
		new JPanel();
		
		java.net.URL logo1 = LoginPass.class
				.getResource("/ch/zbw/erp/image/erp-logo.jpg");
		erplogo = new ImageIcon(logo1);
		image = new JLabel(erplogo);
		falsch= new JLabel("Falsches Password", JLabel.CENTER);
		
		users = new JPanel();
		pass = new JPanel();
		jPlogin = new JPanel();
		
		passwordField = new JPasswordField();
		buttonGroupUsers = new ButtonGroup();
		login = new JButton("Login");
		mitarbeiter = new JRadioButton("Mitarbeiter");
		abteilungsleiter = new JRadioButton("Abteilungsleiter");
		firmenleiter = new JRadioButton("Firmenleiter");
		
		//set Layouts
		setLayout(new GridLayout(2, 0));
		obenImage.setLayout(new GridLayout(1, 0));
		untenPass.setLayout(new GridLayout(3, 0));
		users.setLayout(new FlowLayout());
		pass.setLayout(new FlowLayout());
		passwordField.setPreferredSize(new Dimension(300,50));
		passwordField.setToolTipText("Password eingeben");
		mitarbeiter.setSelected(true);
		jPlogin.setLayout(new GridLayout(2, 0));
		falsch.setVisible(false);
		falsch.setFont(new Font("Arial", Font.BOLD, 18));
		falsch.setForeground(Color.RED);
		pass.setFont(new Font("Arial", Font.BOLD, 24));
		login.setFont(new Font("Arial", Font.BOLD, 18));
		
		mitarbeiter.setFont(new Font("Arial", Font.BOLD, 18));
		abteilungsleiter.setFont(new Font("Arial", Font.BOLD, 18));
		firmenleiter.setFont(new Font("Arial", Font.BOLD, 18));
		
		//set the Actionlistener
		
		login.addActionListener(this);
		passwordField.addActionListener(this);
		
		//add Buttons & Panels
		buttonGroupUsers.add(mitarbeiter);
		buttonGroupUsers.add(abteilungsleiter);
		buttonGroupUsers.add(firmenleiter);
		
		users.add(mitarbeiter,0);
		users.add(abteilungsleiter,1);
		users.add(firmenleiter,2);
		
		pass.add(passwordField,0);
		
		jPlogin.add(login, 0);
		jPlogin.add(falsch, 1);
		
		untenPass.add(users,0);
		untenPass.add(pass,1);
		untenPass.add(jPlogin,2);
		
		obenImage.add(image,0);
		
		add(obenImage,0);
		add(untenPass,1);
		
		setVisible(true);
				
	}
	
	public void setBildAnziegen(boolean visit){
		setVisible(visit);
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(mitarbeiter.isSelected()){
			control.setUser("Mitarbeiter");
			control.setPass(passwordField.getText());
			if(control.dbconnect()){
				setBildAnziegen(false);
				control.nextGUI();
			}else{
				setBildAnziegen(true);
				falsch.setVisible(true);
			}
		}
		
		if(abteilungsleiter.isSelected()){
			control.setUser("Abteilungsleiter");
			control.setPass(passwordField.getText());
			if(control.dbconnect()){
				setBildAnziegen(false);
				control.nextGUI();
			}else{
				setBildAnziegen(true);
				falsch.setVisible(true);
			}
		}
		if(firmenleiter.isSelected()){
			control.setUser("Firmenleiter");
			control.setPass(passwordField.getText());
			if(control.dbconnect()){
				setBildAnziegen(false);
				control.nextGUI();
			}else{
				setBildAnziegen(true);
				falsch.setVisible(true);
			}
		}
	}
}

